﻿using One.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace OneWeb.Repository
{
    public class ArticleRepository : Repository<Article>
    {
        public Article GetToday()
        {
            return DbSet.OrderByDescending(x => x.HpMakeTime).FirstOrDefault();
        }

        public Article GetPrevious()
        {
            Article currentEssay = DbSet.OrderByDescending(x => x.HpMakeTime).FirstOrDefault();
            Article essay = (from c in DbSet
                               where c.HpMakeTime < currentEssay.HpMakeTime
                               orderby c.HpMakeTime descending
                               select c).FirstOrDefault();
            return essay;
        }

        public Article GetNext()
        {
            Article currentEssay = DbSet.OrderByDescending(x => x.HpMakeTime).FirstOrDefault();
            Article essay = (from c in DbSet
                           where c.HpMakeTime > currentEssay.HpMakeTime
                           orderby c.HpMakeTime ascending
                           select c).FirstOrDefault();
            return essay;
        }

        public Article GetToday(int? id)
        {
            Article essay = DbSet.Find(id);
            return essay ?? new Article();
        }

        public Article GetPrevious(int currentId)
        {
            Article currentEssay = DbSet.Find(currentId);
            Article essay = (from c in DbSet
                           where c.HpMakeTime < currentEssay.HpMakeTime
                           orderby c.HpMakeTime descending
                           select c).FirstOrDefault();
            return essay;
        }

        public Article GetNext(int currentId)
        {
            Article currentEssay = DbSet.Find(currentId);
            Article essay = (from c in DbSet
                           where c.HpMakeTime > currentEssay.HpMakeTime
                           orderby c.HpMakeTime ascending
                           select c).FirstOrDefault();
            return essay;
        }
    }
}